package yue.shen.android.demo.select

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.luck.picture.lib.entity.LocalMedia
import yue.shen.android.demo.R

class SelectAdapter(private val data: ArrayList<LocalMedia>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val selectImages = ArrayList<LocalMedia>()
    private var attainMaxSelect = false
    private var selectPicListener: SelectPicListener? = null

    fun getSelectImageData() = selectImages

    fun setSelectPicListener(selectPicListener: SelectPicListener) {
        this.selectPicListener = selectPicListener
    }

    fun updateData(data: ArrayList<LocalMedia>) {
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.item_select_picture, parent, false)
        return SelectImageViewHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = data[position]
        when (holder) {
            is SelectImageViewHolder -> {
                holder.bindData(item)
                if (attainMaxSelect && item.num <= 0) {
                    holder.getImageView().alpha = 0.3f
                } else {
                    holder.getImageView().alpha = 1f
                }
                holder.setNotSelectClick(View.OnClickListener {
                    selectPic(holder, item, true)
                })
                holder.setSelectClick(View.OnClickListener {
                    selectPic(holder, item, false)
                })
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    private fun selectPic(holder: SelectImageViewHolder, item: LocalMedia, add: Boolean) {
        val selectPicSize = selectImages.size
        if (add) {
            if (selectPicSize >= 9) {
                Toast.makeText(holder.getImageView().context, "已经9张了", Toast.LENGTH_LONG).show()
                return
            }
            holder.getSelectView().visibility = View.VISIBLE
            holder.getNotSelectView().visibility = View.GONE
            selectImages.add(item)
            holder.getSelectView().text = "${selectImages.size}"
            item.num = selectImages.size
        } else {
            holder.getSelectView().visibility = View.GONE
            holder.getNotSelectView().visibility = View.VISIBLE
            selectImages.remove(item)
            item.num = -1
        }
        if (selectImages.size == 9) {
            attainMaxSelect = true
            notifyDataSetChanged()
        } else {
            if (attainMaxSelect) {
                attainMaxSelect = false
                notifyDataSetChanged()
            }
        }
        selectPicListener?.attainSelectCount(selectImages.size > 0)
    }

}